import {observable,action, toJS} from 'mobx';
import {Tools,Bubble,Toast} from 'weaver-mobile';
import { RouteLayout,Form,SearchAdvanced,ListViewPage } from 'weaver-mobile-page';
import * as API from '../../api/home/wallet';

const getLabel = Tools.getLabel;

//高级搜索
const initAdvancedItem = {
    form: new Form.Store(),
    parentPath: "",
    conditioninfo: [],
    formValue: {},
}

export default class WalletStore {
    /********************************************************/
    @observable loading = false;
    @observable hasRight = false;

	@observable tabs = [];
    rememberGroup = [
        { title: getLabel(97,'日期') ,workflowid:'',key: "billingdate",mark:0 },
        { title: getLabel(534,'金额'),workflowid:'',key: "taxincludedprice",mark:'' },
        { title: getLabel(131005,'销售方') ,workflowid:'',key: "seller",mark:'' },
    ];
    @observable selectKeyTop = 0;
    @observable mark = {};

    @observable nameQuery = '';
    @observable routeData = new RouteLayout.Store();
    @observable invoiceDetails = [];
    @observable isPdf = false;
    @observable form = {};
    @observable options = [];

    @observable workflowSelect = [];
    @observable wfInfo = [];

    @observable stateParams = {
        isSelect:false,
        onRowSelect:[],
        allMoney:0
    };

    @observable listViewPageData = new ListViewPage.Store({ id: '/listViewPage0', route: '/wallet/noSubmit' });
    layout = new RouteLayout.Store({ route: '/wallet/noSubmit' });

    @observable nowPath = '/wallet/noSubmit';
    //高级搜索
    @observable advancedItem = { ...initAdvancedItem };

    @observable dataKey = '';

    /***************发票详情*******************/
    @observable loading_details = false;

    imageId = '';
    documentType = '';
    @observable headeData = [];
    @observable bodyData = [];
    @observable detailsArr = [];
    showAllDetailsArr = [];

    @observable detailsEditList = {};

    /*            发票云                     */
    @observable cloudInvoiceId = '';


    initStore = (value)=>{
        this.search = new SearchAdvanced.Store({ route:value});
    }

    @action("切换tab后要重新走接口")
    storeChangeTab = (tab,order) =>{
        switch (tab) {
            case 0:this.initData(0,order);break;
            case 1:this.initData(1,order);break;
            case 2:this.initData(2,order);break;
            case 3:this.initData(3,order);break;
            default:
        }
    }

    initData = (type=0,order=0) =>{
        this.getTicketHolderTab();        
        this.mark = this.joinObj(this.rememberGroup,'mark');
        
        let value = {
            status:type,
            orderKey:'billingdate',
            order:order
        }

        let page = '/wallet/noSubmit';
        if(type === 0){
            page = '/wallet/noSubmit';
            this.listViewPageData = new ListViewPage.Store({ id: '/listViewPage0', route: page });
        }else if(type === 1){ 
            page = '/wallet/alreadySubmit';
            this.listViewPageData = new ListViewPage.Store({ id: '/listViewPage1', route: page });
        }else if(type === 2){ 
            page = '/wallet/cancelSubmit';
            this.listViewPageData = new ListViewPage.Store({ id: '/listViewPage2', route: page });
        }

        this.nowPath = page;
        this.layout = new RouteLayout.Store({ route: page, id: "advanceSearch" });
        this.getTicketHolderList(value);
        this.getTicketHolderAS();
    }

    //获取tab页
    getTicketHolderTab = (value={},iframeCallBack) =>{
        this.loading = true;
        API.getTicketHolderTab(value).then( res =>{
            const {status,tabs} = res;
            if(status === '1'){
                this.tabs = [
					{title: getLabel(383090,"未锁定"),key:0,pathname:'/wallet/noSubmit'}, 
                    {title: getLabel(383091,"已锁定"),key:1,pathname:'/wallet/alreadySubmit'}, 
                    {title: getLabel(383092,"已核销"),key:2,pathname:'/wallet/cancelSubmit'}
                ];
            }else{
                Bubble.error(res.errorInfo)
            }

            if(iframeCallBack){
                iframeCallBack();
            }else{
                this.loading = false;
            }
        })
    }    
    
    //获取高级搜搜
    getTicketHolderAS = (value={}) =>{
        API.getTicketHolderAS(value).then( res =>{
            const {status,conditions} = res;
            if(status === '1'){
                if(JSON.stringify(conditions) !== "{}" ){
                    this.advancedItem.form.initForm(res.conditions, this.nowPath +'/advanceSearch');
                    this.advancedItem.conditioninfo = res.conditions;
                    this.options = res.conditions[0].items[0].options;
                }
                
            }else{
                Bubble.error(res.errorInfo)
            }
        })
    }    


    //我的票夹  list
    getTicketHolderList = (value={}) =>{
        this.loading = true;
        API.getTicketHolderList(value).then( res =>{
            const {status,datas,errorInfo} = res;
            if(status == '1'){
                if (datas) { this.dataKey = datas; };
                this.listViewPageData.getData({ dataKey:datas });
            }else{
                Toast.info(errorInfo);
            }
            
            this.loading = false;
        })
    }

    //获取发票详情
    getInvoiceInfo = (value={},copyDetails) =>{
        this.loading_details = true;
        API.getInvoiceInfo(value).then( res =>{
            const {status,invoiceDetails} = res;
            if(status == '1'){
                const {imageId,isPdf,headeData,bodyData,documentType = 'png'} = invoiceDetails;
                this.imageId = imageId;
                this.isPdf = isPdf;
                this.documentType = documentType;
                this.headeData = headeData;
                this.bodyData = bodyData;

                this.detailsArr = bodyData[0].details;
                
                this.detailsEditList = res.detailsEditList;
                
                if(copyDetails){
                    let copy =  JSON.parse(JSON.stringify(this.detailsArr));
                    copy.map( (items,index)=>{
                        items.show = true;
                    });
                    this.showAllDetailsArr = copy;
                }
            }else{
                Bubble.error(res.errorInfo)
            }
            this.loading_details = false;
        })
    }

    //获取 报销流程 选项
    getInvoiceCreatWorkflow = (value={},callBack) =>{
        // this.loading = true;
        API.getInvoiceCreatWorkflow(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status == '1'){
                this.workflowSelect = workflowSelect;
                callBack && callBack({workflowSelect:workflowSelect});
            }else{
                Bubble.error(res.errorInfo)
            }

            // this.loading = false;
        })
    }

    //发起流程
    invoiceCreatWorkflow = (value,callBack) =>{
        this.loading = true;
        API.invoiceCreatWorkflow(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status == '1'){
                callBack && callBack({
                    requestId:res.requestId
                })
            }else{
                Bubble.error(res.errorInfo)
            }

            this.loading = false;
        })
    }   
    
    //删除 发票
    doDeleteInvoice = (value,callBack) =>{
        this.loading_details = true;
        API.doDeleteInvoice(value).then(res=>{
            const {status,errorInfo,} = res;
            if(status == '1'){
                Bubble.success(getLabel(20461, "删除成功"));
                callBack && callBack();
            }else{
                Bubble.error(errorInfo);
            }
            this.loading_details = false;
        })
    }

    //设置原state参数
    setStateParams = (value) =>{
        this.stateParams = Object.assign(this.stateParams,value)
    }

    setSelectKeyTop = (value) =>{
        this.selectKeyTop = value;
    }

    //根据传出来的数据拼接 key - value
    joinObj = (data,v) =>{
        let _obj = {};
        data.map( item =>{
            _obj[item.key] = item[v]
        })
      
        return _obj
    }

    setMark = (value={},initMark) =>{
        if(initMark){
            this.mark = {billingdate: 0, taxincludedprice: '', seller: ''}
        }else{
            this.mark = Object.assign(this.mark,{...value});
        }

    }   
    
    setNameQuery = (value='') =>{
        this.nameQuery = value;
    }

    //改变详情数据
    setDetailsArr = (value=[]) =>{
        this.detailsArr = value;
    }

    setBodyData = (value) =>{
        this.bodyData[0].details = value;
    }

    //重置 form表单
    resetForm = () =>{
        this.search.form.resetFields();
    }

    /*      ****************************    跳转票夹      ***********************      */
    //跳转票夹 高级搜索
    getToTicketHolderAS = (value={}) =>{
        API.getTicketHolderAS(value).then( res =>{
            const {status,conditions} = res;
            if(status === '1'){
                if(JSON.stringify(conditions) !== "{}" ){
                    this.search.form.initForm(res.conditions, this.search.path);
                    this.search.conditioninfo = res.conditions;
                    this.options = res.conditions[0].items[0].options;
                }
                
            }else{
                Bubble.error(res.errorInfo)
            }
        })
    }
    

    //保存流程相关
    setWfInfo = (value) =>{
        this.wfInfo = value;
    }

    @action
    changeSearchValue = (val) => {
        this.nameQuery = val;
    };

    setLoading = (value) =>{
        this.loading = value;
    }

    clearAllData = ()=>{
        this.nameQuery = '';
        this.selectKeyTop = 0;
        this.listViewPageData = new ListViewPage.Store({ id: '', route: '' });
        this.stateParams = {  isSelect:false, onRowSelect:[],allMoney:0 };

    }

    /* ********************发票云*********************  */
    setCloudInvoiceId = (value) =>{
        this.cloudInvoiceId = value;
    }

}